//
//  CSTool.m
//  ChinaShip
//
//  Created by Yale on 2019/2/22.
//  Copyright © 2019年 ChinaShip. All rights reserved.
//

#import "CSTool.h"

@implementation CSTool
+ (void)saveSwitchState:(BOOL)state{
    if (!state) {
        state = NO;
    }
    [[NSUserDefaults standardUserDefaults] setBool:state forKey:@"lineSwitch"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (BOOL)getSwitchState{
    return [[NSUserDefaults standardUserDefaults] boolForKey:@"lineSwitch"];
}

+ (NSMutableArray *)getAllUsers{
    NSMutableArray *tempUsers = [NSMutableArray new];
    NSMutableArray *userArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"allUsers"];
    if (!userArray || userArray.count == 0) {
        [tempUsers addObject:@{
                               @"user":@"cssc",
                               @"pwd" :@"cssc2019"
                                   }];
        [[NSUserDefaults standardUserDefaults] setObject:tempUsers forKey:@"allUsers"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }else{
        [tempUsers addObjectsFromArray:userArray];
    }
    return tempUsers;
}

+ (void)addUser:(NSDictionary *)userInfo{
    NSArray *userArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"allUsers"];
    NSMutableArray *tempArray = [NSMutableArray new];
    [tempArray addObjectsFromArray:userArray];
    [tempArray addObject:userInfo];
    [[NSUserDefaults standardUserDefaults] setObject:tempArray forKey:@"allUsers"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (void)saveLoginState{
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"LoginSuccess"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (BOOL)isLogin{
    return [[NSUserDefaults standardUserDefaults] boolForKey:@"LoginSuccess"];
}

@end
